//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing
{
    public class GroupShape : IAnchored, IOfficeXmlSerializableAs
    {
        private GroupShapeProperties grpSpPr;
        private List<IOfficeXmlSerializable> items = new List<IOfficeXmlSerializable>();
        private GroupShapeNonVisual nvGrpSpPr;

        internal GroupShapeNonVisual GroupShapeNonVisual
        {
            get { return nvGrpSpPr; }
            set { nvGrpSpPr = value; }
        }

        public GroupShapeProperties GroupShapeProperties
        {
            get { return grpSpPr; }
            set { grpSpPr = value; }
        }

        public List<IOfficeXmlSerializable> Items
        {
            get { return items; }
        }

        #region IAnchored Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (var c = context.Read("grpSp"))
            {
                GroupShapeNonVisual = c.ReadElement<GroupShapeNonVisual>();
                GroupShapeProperties = c.ReadElement<GroupShapeProperties>();
                //\\c.Data = new IsChildMarker();
                IOfficeXmlSerializable item = c.GetData<IShapeReader>().ReadContent(c);
                while (item != null)
                {
                    Items.Add(item);
                    item = c.GetData<IShapeReader>().ReadContent(c);
                }
                foreach (IOfficeXmlSerializable it in Items)
                {
                    if (it is ShapeBase)
                    {
                        ShapeBase shape = (ShapeBase) it;
                        shape.ShapeProperties.SafeXfrm.Size.Width *= GroupShapeProperties.Transform.Size.Width/
                                                                 GroupShapeProperties.Transform.ChildSize.Width;
                        shape.ShapeProperties.SafeXfrm.Size.Height *= GroupShapeProperties.Transform.Size.Height /
                                                                  GroupShapeProperties.Transform.ChildSize.Height;
                        shape.ShapeProperties.SafeXfrm.Offset.X =
                            (shape.ShapeProperties.SafeXfrm.Offset.X - GroupShapeProperties.Transform.ChildOffset.X) *
                            GroupShapeProperties.Transform.Size.Width/GroupShapeProperties.Transform.ChildSize.Width;
                        shape.ShapeProperties.SafeXfrm.Offset.Y =
                            (shape.ShapeProperties.SafeXfrm.Offset.Y - GroupShapeProperties.Transform.ChildOffset.Y)*
                            GroupShapeProperties.Transform.Size.Height/GroupShapeProperties.Transform.ChildSize.Height;
                    }
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("grpSp"))
            {
                c.WriteElement(GroupShapeNonVisual);
                c.WriteElement(GroupShapeProperties);
                //\\c.Data = new IsChildMarker();
                foreach (IOfficeXmlSerializable item in Items)
                    c.WriteElement(item);
            }
        }

        #endregion

        void IOfficeXmlSerializableAs.ReadXmlAs(string name, ReadContext context)
        {
            using (var c = context.Read(name))
            {
                GroupShapeNonVisual = c.ReadElement<GroupShapeNonVisual>();
                GroupShapeProperties = c.ReadElement<GroupShapeProperties>();
                //\\c.Data = new IsChildMarker();
                IOfficeXmlSerializable item = c.GetData<IShapeReader>().ReadContent(c);
                while (item != null)
                {
                    Items.Add(item);
                    item = c.GetData<IShapeReader>().ReadContent(c);
                }
                foreach (IOfficeXmlSerializable it in Items)
                {
                    if (it is ShapeBase)
                    {
                        ShapeBase shape = (ShapeBase)it;
                        shape.ShapeProperties.SafeXfrm.Size.Width *= GroupShapeProperties.Transform.Size.Width /
                                                                 GroupShapeProperties.Transform.ChildSize.Width;
                        shape.ShapeProperties.SafeXfrm.Size.Height *= GroupShapeProperties.Transform.Size.Height /
                                                                  GroupShapeProperties.Transform.ChildSize.Height;
                        shape.ShapeProperties.SafeXfrm.Offset.X =
                            (shape.ShapeProperties.SafeXfrm.Offset.X - GroupShapeProperties.Transform.ChildOffset.X) *
                            GroupShapeProperties.Transform.Size.Width / GroupShapeProperties.Transform.ChildSize.Width;
                        shape.ShapeProperties.SafeXfrm.Offset.Y =
                            (shape.ShapeProperties.SafeXfrm.Offset.Y - GroupShapeProperties.Transform.ChildOffset.Y) *
                            GroupShapeProperties.Transform.Size.Height / GroupShapeProperties.Transform.ChildSize.Height;
                    }
                }
            }
        }

        void IOfficeXmlSerializableAs.WriteXmlAs(string name, WriteContext context)
        {
            using (WriteContext c = context.Write(name))
            {
                c.WriteElement(GroupShapeNonVisual);
                c.WriteElement(GroupShapeProperties);
                //\\c.Data = new IsChildMarker();
                foreach (IOfficeXmlSerializable item in Items)
                    c.WriteElement(item);
            }
        }
    }
}